package eclipse.weibo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;

import weibo4j.DirectMessage;
import weibo4j.util.DebugPrn;
import eclipse.weibo.modle.WeiboView;

public class ShowMessageObserver implements Observer {
    private static DebugPrn dp = new DebugPrn(ShowMessageObserver.class.getName());
    IWorkbenchPage page = null;
    private Display display = null;

    public ShowMessageObserver(IWorkbenchPage page, Display display) {
        this.page = page;
        this.display = display;
    }

    public void update(Observable o, Object arg) {
        List<?> list = (List<?>) arg;
        ArrayList<DirectMessage> al = new ArrayList<DirectMessage>();
        for (int i = 0; i < list.size(); i++) {
            al.add((DirectMessage) list.get(i));
        }
        TTHread t = new TTHread(al);
        this.display.asyncExec(t);
    }

    class TTHread extends Thread {
        List<DirectMessage> list = null;

        public TTHread(List<DirectMessage> list) {
            this.list = list;
        }

        public void run() {
            try {
                IViewPart viewPart = ShowMessageObserver.this.page.findView("eclipse.weibo.modle.WeiboView");
                if (viewPart == null) {
                    viewPart = ShowMessageObserver.this.page.showView("eclipse.weibo.modle.WeiboView");
                }
                ShowMessageObserver.this.page.showView("eclipse.weibo.modle.WeiboView");
                ((WeiboView) viewPart).showMyMessage(this.list);
                viewPart.setFocus();
            } catch (Exception e) {
                dp.error(e, e);
            }
        }
    }
}
